package com.fenghongzhang.day008view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
    /**
     * 描边的原理, 就是在划过的地方,在用不同的颜色画一遍
     */
    Paint paint3 = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int progress = 0;
    private int time = 5;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.RED);
//
//        canvas.drawCircle(100,100,80,paint);
//        canvas.drawCircle(100,100,70,paint2);

        //空心
        paint.setStyle(Paint.Style.STROKE);
        //线的宽度
        paint.setStrokeWidth(10);

        //矩形区域
        RectF rectF = new RectF(200, 200, 600, 600);


        //空心
        paint3.setStyle(Paint.Style.STROKE);
        //线的宽度
        paint3.setStrokeWidth(10);
        paint3.setColor(Color.BLUE);
        canvas.drawArc(rectF,270,360,false,paint3);


        //矩形
//        canvas.drawRect(rectF,paint2);
        //文字居中
        paint2.setTextAlign(Paint.Align.CENTER);
        //文字大小
        paint2.setTextSize(80);

        //写一个文字
        canvas.drawText(time+"",rectF.centerX(),rectF.centerY()+20,paint2);

        //画一个圆弧
        canvas.drawArc(rectF,270,progress,false,paint);



    }

    public void start(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 1; i <= 360; i++) {
                    try {
                        Thread.sleep(20);
                        progress = i;
                        //为啥是72呢
                        if(progress % 72 == 0){
                            time--;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    MyView.this.postInvalidate();
                }
            }
        }).start();
    }
}

